﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IMDTO;

namespace IMDAL
{
    public class IMDALName
    {
        public int WriteName(IMDTOName name)
        {
            try
            {

                
                InMedioEntities db = new InMedioEntities();



                Name nm = (from n in db.Name
                           where (n.CallName == name.CallName && n.FirstName == name.FirstName && n.Initial == name.Initial
                           && n.LastName == name.LastName && n.MiddleName == name.MiddleName)
                           select n).FirstOrDefault();

                if (nm != null)
                {
                    return nm.idName;
                }

                Name naam = new Name();
                naam.CallName = name.CallName;
                naam.FirstName = name.FirstName;
                naam.Initial = name.Initial;
                naam.LastName = name.LastName;
                naam.MiddleName = name.MiddleName;
                naam.UserCrea = "jk";
                naam.TsCrea = DateTime.Now;


                db.AddToName(naam);
                db.SaveChanges();

                return naam.idName;

            }
            catch (Exception e)
            {

                throw e;
            }
        }

    }
}
